// pages/product-presentation/product-presentation.js
import {
    getadphone
} from '../../api/app'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isup: false,
        phonelist: [],
        type: ''
    },
    onOpenpopup() {
        this.setData({
            isup: true
        })
    },
    onClosePopup() {
        this.setData({
            isup: false
        })
    },
    callPhone(phoneNumber) {
        wx.makePhoneCall({
            phoneNumber: phoneNumber,
            success: (res) => {
                console.log(111, res);
            },
            fail: function (res) {
                console.log(222, res);
            }
        })
    },
    showPhone() {
        let arr = []
        if (this.data.phonelist.length !== 0) {
            this.data.phonelist.forEach((ele, index) => {
                arr.push(ele.phone + '-' + ele.name)
            })
        } else {
            arr.push('15950927000')
        }

        wx.showActionSheet({
            itemList: arr,
            success: (res) => {
                if (this.data.phonelist.length !== 0) {
                    this.callPhone(this.data.phonelist[res.tapIndex].phone + '')
                } else {
                    this.callPhone('15950927000')
                }
            },
            fail(res) {
                console.log(res.errMsg)
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            type: options.type
        })
        console.log(options);
        if (options.type === '1') {
            wx.setNavigationBarTitle({
                title: '发薪专户'
            })
        } else if (options.type === '2') {
            wx.setNavigationBarTitle({
                title: '长期保'
            })
        } else if (options.type === '3') {
            wx.setNavigationBarTitle({
                title: '日日保'
            })
        } else if (options.type === '4') {
            wx.setNavigationBarTitle({
                title: '团意日保'
            })
        } else if (options.type === '5') {
            wx.setNavigationBarTitle({
                title: '电子签'
            })
        }
    },
    onToSubscribe() {
        wx.navigateTo({
            url: '/pages/subscribe/subscribe',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        getadphone().then((res) => {
            console.log(res, 'yfsudahiygiuyh');
            if (res.length === 0) {
                return
            } else {
                this.data.phonelist = res.map((ele, index) => {
                    return {
                        name: ele.salesName,
                        phone: ele.phone
                    }
                })
                this.setData({
                    phonelist: this.data.phonelist
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})